package controller;

import com.alibaba.fastjson.JSON;
import dao.UserInfoDao;
import dao.impl.UserInfoDaoImpl;
import entity.LayUiTable;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/3/23 11:43
 * @description：用户信息控制器
 * @modified By：
 * @version:
 */
@WebServlet(urlPatterns = "/userinfo",loadOnStartup = 1)
public class UserInfoServlet  extends HttpServlet {
    UserInfoDao userInfoDao = new UserInfoDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有的请求参数和参数值，Map(key,value)
        Map<String,String>  paramMap= new HashMap();
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String key = parameterNames.nextElement();
            String value = req.getParameter(key);
            paramMap.put(key,value);
        }
        String method = paramMap.get("method");
        Integer id=0;
        switch (method){
            case "add":
                userInfoDao.addUserInfo(paramMap);
                this.findAll(req,resp);
                break;

            case "del":
                 id =Integer.valueOf(paramMap.get("id"));
                userInfoDao.deleteUserInfoById(id);
                //删除之后，重新进入查询所有
                this.findAll(req,resp);
                break;
            case "toUpdate":
                //去修改页面，带上id对应的用户信息
                id =Integer.valueOf(paramMap.get("id"));
                 //根据id查询用户信息
                List<Map<String, Object>> mapList = userInfoDao.findUserInfoById(id);
                if(null!=mapList&&mapList.size()>0){
                    Map<String, Object> userinfo = mapList.get(0);
                    req.setAttribute("userinfo",userinfo);
                    req.getRequestDispatcher("updateUser.jsp").forward(req,resp);
                }
                break;
            case "update":
                //修改操作
                userInfoDao.updateUserInfo(paramMap);
                this.findAll(req,resp);
                break;
            case "findAll":
                this.findAll(req,resp);
                break;
            case "findAllJson":
                List<Map<String, Object>> allUser = userInfoDao.findAllUserByCondition(paramMap);
                LayUiTable layUiTable = new LayUiTable();
                layUiTable.setCode(0);
                //设置总条数
                Long count = userInfoDao.getCount();
                layUiTable.setCount(count);
                layUiTable.setData(allUser);
//                resp.getWriter().write(JSON.toJSONString(layUiTable));
                resp.getWriter().write(JSON.toJSONStringWithDateFormat(layUiTable,"yyyy-MM-dd HH:mm:ss"));
                break;
            case "findUserNameById":
                //接收ajax请求
                id =Integer.valueOf(paramMap.get("id"));
                //根据id查询用户信息
              List<Map<String,Object>> userList= userInfoDao.findUserInfoById(id);
              if(null!=userList&&userList.size()>0){
                  Map<String, Object> objectMap = userList.get(0);
                 // Object username = objectMap.get("username");
                  //使用fastjson将java对象转换成json字符串响应给前端
                  String jsonString = JSON.toJSONString(objectMap);
                  resp.getWriter().write(jsonString);
              }
                break;
            default:
                //其他操作
                break;

        }
    }
    private void  findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Map<String, Object>> allUser = userInfoDao.findAllUser();
        req.setAttribute("allUser",allUser);
        req.getRequestDispatcher("success.jsp").forward(req,resp);
    }
}
